package com.frxuexi.mdm2201.view

import android.graphics.Paint
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.frxuexi.apps.FilePage
import com.frxuexi.apps.NetAppPage
import com.frxuexi.mdm2201.R
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

sealed class NavItem(val route: String, val title: String, val img: Int) {
    object MessNav : NavItem("message", "消息", R.drawable.icon1275)
    object HomeNav : NavItem("home", "门户", R.drawable.icon64)
    object MineNav : NavItem("my", "我的", R.drawable.ic_action_account)
}

@Composable
fun MainPage() {
    val navController = rememberNavController()
    val list = listOf<NavItem>(NavItem.MessNav, NavItem.HomeNav, NavItem.MineNav)
    /*var isShow by remember {
        mutableStateOf(true)
    }*/
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentDestination = navBackStackEntry?.destination
    Scaffold(bottomBar = {
        if (currentDestination?.hierarchy?.any {
                list.map { navBarItem -> navBarItem.route }
                    .contains(it.route)
            } == true) {
            NavigationBar {

                list.forEach { nav ->
                    NavigationBarItem(
                        selected = currentDestination?.hierarchy?.any { it.route == nav.route } == true,
                        onClick = {
                            navController.navigate(nav.route) {
                                popUpTo(navController.graph.findStartDestination().id) {
                                    saveState = true
                                }
                                launchSingleTop = true
                                restoreState = true
                            }
                        },
                        icon = {
                            Icon(
                                modifier = Modifier.size(25.dp),
                                painter = painterResource(id = nav.img),
                                contentDescription = ""
                            )
                        },
                        label = { Text(text = nav.title) })
                }
            }
        }
    }, content = { paddingValues ->
        NavHost(
            navController = navController,
            startDestination = "home",
            modifier = Modifier.padding(paddingValues = paddingValues)
        ) {
            composable("message") { MessagePage() }
            composable("home") {
                HomePage(navController = navController)/*{homeEntity->

                isShow=false
                if(homeEntity.name=="在线应用"){
                    navController.navigate("online")
                }else if(homeEntity.name=="文件"){
                    navController.navigate("file")
                }
            }*/

            }
            composable("my") { MyPage() }
            /*composable("online") {
                NetAppPage()
            }*/
//            composable("file") { FilePage() }
        }

    })
}

@Composable
fun MessagePage() {
    Text(text = "消息页面")
}



